<?php

$__FILEPATH__ = dirname(__FILE__)."/";
require_once($__FILEPATH__."../func_common.php");
require_once($__FILEPATH__."../class_def.php");
require_once($__FILEPATH__."../conf/info.php");

CheckAdminAndInitHeader();

$PageArray = array();
$mysqli = GetMysqliInstance();

if(isset($_POST["update"]))
{
	if (isset($_POST["username"]) && isset($_POST["award"]))
	{
		$username = $_POST["username"];
		$award = $_POST["award"];
		$stmt = $mysqli->prepare("update acmerlist set award = ? where username = ?");
		$stmt->bind_param("ss", $award, $username);
		if ($stmt->execute())
			echo "<script type=\"text/javascript\">window.alert(\"更新成功！\")</script>";
		$stmt->close();
	}
}
else if (isset($_POST["delete"]))
{
	if (isset($_POST["username"]))
	{
		$username = $_POST["username"];
		$stmt = $mysqli->prepare("select imagesrc from acmerlist where username = ?");
		$stmt->bind_param("s", $username);
		$stmt->execute();
		$stmt->bind_result($imagesrc);
		if ($stmt->fetch())
		{
			$stmt2 = $mysqli->prepare("delete from acmerlist where username = ?");
			$stmt2->bind_param("s", $username);
			$stmt2->execute();
			$stmt2->close();
			if (file_exists($imagesrc))
			{
				unlink($imagesrc);
			}
		}
		$stmt->close();

		
	}
}

$PageArray["acmerlist"] = array();

if (isset($_POST["keyword"]) && $_POST["keyword"] != "")
{
	$keyword = $_POST["keyword"];
	$keywordstr = "%".$keyword."%";
	$keywordnum = $keyword."%";
	$stmt = $mysqli->prepare("select * from acmerlist where username like ? or nickname like ? or studentnum like ?");
	$stmt->bind_param("sss", $keywordstr, $keywordstr, $keywordnum);
	$stmt->execute();
	$stmt->bind_result($r_username, $r_nickname, $r_studentnum, $r_award);
	while($stmt->fetch())
	{
		$AcmerItem = new AcmerListItem();
		$AcmerItem->username 	= $r_username;
		$AcmerItem->nickname 	= $r_nickname;
		$AcmerItem->studentnum 	= $r_studentnum;
		$AcmerItem->award 	 	= $r_award;
		$PageArray["acmerlist"][] = $AcmerItem;
	}
	$stmt->close();
}
else 
{
	$result = $mysqli->query("select * from acmerlist limit 0, 10");
	if ($result)
	{
		while($row = $result->fetch_array(MYSQLI_ASSOC))
		{
			$AcmerItem = new AcmerListItem();
			$AcmerItem->username 	= $row["username"];
			$AcmerItem->nickname 	= $row["nickname"];
			$AcmerItem->studentnum 	= $row["studentnum"];
			$AcmerItem->award 	 	= $row["award"];
			$PageArray["acmerlist"][] = $AcmerItem;
		}
		$result->close();
	}
}



$mysqli->close();

?>